<?php

namespace app\common\auth;

use app\common\model\Admin;
use app\common\model\AuthGroupAccess;
use app\common\model\AuthRule;

class AdminAuth
{
    protected static $instance;//实例
    protected $isLogin = false;//是否登录
    protected $model;//模型
    protected $_user;//用户信息
    public $id;//用户ID
    protected $message = "";//错误信息
    protected $session_key = "admin_id";
    public $username;//用户名
    protected $breadcrumb;
    public function __construct()
    {
        $this->model = new Admin();
    }
    public static function getMessage()
    {
        return self::instance()->message;
    }
    public function getError()
    {
        return $this->message;
    }
    public static function instance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new static();
        }
        return self::$instance;
    }
    public function isLogin()
    {
        $session = session($this->session_key);
//        echo "<pre>";
//        print_r($session);
//        exit;
        if (!empty($session)) {
            $this->isLogin = true;
            $this->id = $session;
            $this->_user = $this->model->where('id', $session)->find();
            $this->username = $this->_user['username'];
        }
        return $this->isLogin;
    }
    public function login($username, $password)
    {
//        session_start();
        $row = $this->model->where('username', $username)->find();
        if(empty($row)){
            $this->message = "用户名不存在";
            return false;
        }
        if(check_password(trim($password), $row['password'],$row['salt'])){
            $this->isLogin = true;
            $this->id = $row['id'];
            $this->_user = $row;
            session($this->session_key, $row['id']);
            $this->model->where('id', $row['id'])->update(['logintime' => time(), 'loginip' => request()->ip()]);
            return true;
        }else{
            $this->message = "密码错误";
            return false;
        }
    }
    public function getUser()
    {
        return $this->_user;
    }
    public function getRule($ismenu = false)
    {
        $userGeoup = AuthGroupAccess::where('uid', $this->id)
            ->select();
        $resAll = [];
        foreach ($userGeoup as $v){
            $res = $v->group($ismenu);
            if(!empty($res)){
                $resAll = array_merge($resAll,$res);
            }
        }
        $resAll = array_unique($resAll);
        $rules = AuthRule::where('id', 'in', $resAll)
            ->where('status', 1)
            ->withoutField('updatetime')
            ->order('id asc,weigh desc')
            ->select()->toArray();
        if(empty($rules)){
            return [];
        }
        $min = min(array_column($rules, 'pid'));
        foreach ($rules as &$v){
            $v['title'] = __($v['title']);
            $v['module'] = get_app_map($v['module'],false);
        }
        unset($v);
        $rules = list_to_tree($rules, 'id', 'pid', 'children', $min);
        return $rules;
    }
    public function logout()
    {
        session($this->session_key, null);
        return true;
    }
    public function getBreadCrumb($path = '')
    {
        if ($this->breadcrumb || !$path) {
            return $this->breadcrumb;
        }
        $titleArr = [];
        $menuArr = [];
        $urlArr = explode('/', $path);
        foreach ($urlArr as $index => $item) {
            $pathArr[implode('/', array_slice($urlArr, 0, $index + 1))] = $index;
        }
        echo "<pre>";
        print_r($pathArr);
        exit;
//        if (!$this->rules && $this->id) {
//            $this->getRuleList();
//        }
        foreach ($this->rules as $rule) {
            if (isset($pathArr[$rule['name']])) {
                $rule['title'] = __($rule['title']);
                $rule['url'] = url($rule['name']);
                $titleArr[$pathArr[$rule['name']]] = $rule['title'];
                $menuArr[$pathArr[$rule['name']]] = $rule;
            }

        }
        ksort($menuArr);
        $this->breadcrumb = $menuArr;
        return $this->breadcrumb;
    }
}